#include "stdafx.h"
#include "LayoutButton.h"
#include "FCDraw.h"

LayoutButton::LayoutButton(){
    FCFont tFont(L"Default", 14);
    setFont(&tFont);
}

LayoutButton::~LayoutButton(){
}

void LayoutButton::onPaint(FCPaint *paint, const FCRect& clipRect){
    Long drawColor = FCColor::rgb(50, 50, 50);
    if (MyColor::getStyle() == 0)
    {
        drawColor = FCColor::rgb(150, 150, 150);
    }
    if (this == m_native->getHoveredView())
    {
        drawColor = FCColor::rgb(42, 138, 195);
    }
    int width = getWidth(), height = getHeight();
    FCDiv *parentDiv = dynamic_cast<FCDiv*>(getParent());
    if(parentDiv->getVScrollBar() && parentDiv->getVScrollBar()->isVisible()){
        width -= 8;
    }
    FCRect drawRect = {2, 2, width - 2, height - 2};
    paint->drawRoundRect(drawColor, 1, 0, drawRect, 8);
    String text = getText();
    FCRect iRect = {5, 5, 5 + height - 10, height - 5};
    drawDesignerIcon(paint, text, iRect, 0, drawColor, drawColor);
    FCFont *tFont = getFont();
    FCSize tSize = paint->textSize(text, tFont);
    drawText(paint, text, drawColor, tFont, iRect.right + 2, (height - tSize.cy) / 2);
}

void LayoutButton::onPaintBorder(FCPaint *paint, const FCRect& clipRect){
}

FCSize LayoutButton::drawText(FCPaint *paint, String text, Long dwPenColor, FCFont *font, int x, int y){
    FCSize tSize = paint->textSize(text, font, -1);
    FCRect tRect = {x, y, x + tSize.cx, y + tSize.cy};
    paint->drawText(text, dwPenColor, font, tRect, -1);
    return tSize;
}

void LayoutButton::drawDesignerIcon(FCPaint *paint, String cName, FCRect drawRect, int tick, Long fillColor, Long drawColor){
    FCRect imageRect = drawRect;
    int sub = (drawRect.right - drawRect.left) / 5;
    imageRect.left += sub;
    imageRect.top += sub;
    imageRect.right -= sub;
    imageRect.bottom -= sub;
    if (cName == L"Button")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        FCFont tFont(L"Default", 14, true, false, false);
        drawText(paint, L"ab", drawColor, &tFont, imageRect.left + 2, imageRect.top + 2);
    }
    else if (cName == L"Calendar")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        FCRect rect1 = {imageRect.left + 12, imageRect.top + 12, imageRect.left + 17, imageRect.top + 18};
        paint->fillRect(FCColor::rgb(255, 80, 80), rect1);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 12, imageRect.right - 2, imageRect.top + 12);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 18, imageRect.right - 2, imageRect.top + 18);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 6, imageRect.top, imageRect.left + 6, imageRect.bottom - 2);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 12, imageRect.top, imageRect.left + 12, imageRect.bottom - 2);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 18, imageRect.top, imageRect.left + 18, imageRect.bottom - 2);
        FCRect rect2 = {imageRect.left, imageRect.top, imageRect.right - 1, imageRect.top + 8};
        paint->fillRect(fillColor, rect2);
    }
    else if (cName == L"CheckBox")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        FCRect innerRect = imageRect;
        innerRect.left += 3;
        innerRect.top += 3;
        innerRect.right -= 3;
        innerRect.bottom -= 3;
        paint->fillRect(fillColor, innerRect);
    }
    else if (cName == L"Div")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
    }
    else if (cName == L"Blank")
    {
        paint->drawRect(fillColor, 1, 1, imageRect);
    }
    else if (cName == L"Label")
    {
        FCFont tFont(L"Default", 24, true, false, false);
        drawText(paint, L"A", fillColor, &tFont, imageRect.left + 4, imageRect.top);
    }
    else if (cName == L"LinkLabel")
    {
        FCFont tFont(L"Default", 24, true, false, false);
        drawText(paint, L"A", fillColor, &tFont, imageRect.left + 4, imageRect.top);
        paint->drawLine(fillColor, 2, 0, imageRect.left + 4, imageRect.bottom + 5, imageRect.right - 4, imageRect.bottom + 5);
    }
    else if (cName == L"RadioButton")
    {
        paint->drawEllipse(fillColor, 1, 0, imageRect);
        FCRect innerRect = imageRect;
        innerRect.left += 3;
        innerRect.top += 3;
        innerRect.right -= 3;
        innerRect.bottom -= 3;
        paint->fillEllipse(fillColor, innerRect);
    }
    else if (cName == L"SplitLayoutDiv")
    {
        paint->drawLine(fillColor, 1, 0, imageRect.left + 12, imageRect.top, imageRect.left + 12, imageRect.bottom - 1);
        paint->drawRect(fillColor, 1, 0, imageRect);
    }
    else if (cName == L"TextBox")
    {
        FCFont tFont(L"Default", 14, true, false, false);
        paint->drawRoundRect(fillColor, 1, 0, imageRect, 0);
        drawText(paint, L"ab|", fillColor, &tFont, imageRect.left, imageRect.top + 2);
    }
    else if (cName == L"ComboBox")
    {
        FCRect rect1 = {imageRect.right - 5, imageRect.top, imageRect.right, imageRect.top + 5};
        paint->fillRect(fillColor, rect1);
        FCRect rect2 = {imageRect.left, imageRect.top, imageRect.right, imageRect.top + 6};
        paint->drawRect(fillColor, 1, 0, rect2);
        FCRect rect3 = {imageRect.left + 6, imageRect.top + 5, imageRect.right, imageRect.bottom};
        paint->drawRect(fillColor, 1, 0, rect3);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 9, imageRect.top + 10, imageRect.right - 4, imageRect.top + 10);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 9, imageRect.top + 14, imageRect.right - 4, imageRect.top + 14);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 9, imageRect.top + 18, imageRect.right - 4, imageRect.top + 18);
    }
    else if (cName == L"DatePicker")
    {
        FCRect rect1 = {imageRect.right - 5, imageRect.top, imageRect.right, imageRect.top + 5};
        paint->fillRect(fillColor, rect1);
        FCRect rect2 = {imageRect.left, imageRect.top, imageRect.right, imageRect.top + 6};
        paint->drawRect(fillColor, 1, 0, rect2);
        FCRect rect3 = {imageRect.left + 5, imageRect.top + 5, imageRect.right - 1, imageRect.top + 10};
        paint->fillRect(fillColor, rect3);
        FCRect rect4 = {imageRect.left + 5, imageRect.top + 5, imageRect.right, imageRect.bottom};
        paint->drawRect(fillColor, 1, 0, rect4);
        FCRect rect5 = {imageRect.left + 14, imageRect.top + 13, imageRect.left + 18, imageRect.top + 17};
        paint->fillRect(FCColor::rgb(255, 80, 80), rect5);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 5, imageRect.top + 13, imageRect.right - 1, imageRect.top + 13);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 5, imageRect.top + 17, imageRect.right - 1, imageRect.top + 17);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 5, imageRect.top + 21, imageRect.right - 1, imageRect.top + 21);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 9, imageRect.top + 10, imageRect.left + 9, imageRect.bottom - 1);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 14, imageRect.top + 10, imageRect.left + 14, imageRect.bottom - 1);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 19, imageRect.top + 10, imageRect.left + 19, imageRect.bottom - 1);
    }
    else if (cName == L"Grid")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 11, imageRect.right - 2, imageRect.top + 11);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 16, imageRect.right - 2, imageRect.top + 16);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 6, imageRect.top, imageRect.left + 6, imageRect.bottom - 2);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 12, imageRect.top, imageRect.left + 12, imageRect.bottom - 2);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 18, imageRect.top, imageRect.left + 18, imageRect.bottom - 2);
        FCRect rect1 = {imageRect.left, imageRect.top, imageRect.right - 1, imageRect.top + 6};
        paint->fillRect(fillColor, rect1);
    }
    else if (cName == L"GroupBox")
    {
        FCPoint points[6] = {0};
        points[0].x = imageRect.left + 4;
        points[0].y = imageRect.top;
        points[1].x = imageRect.left;
        points[1].y = imageRect.top;
        points[2].x = imageRect.left;
        points[2].y = imageRect.bottom;
        points[3].x = imageRect.right;
        points[3].y = imageRect.bottom;
        points[4].x = imageRect.right;
        points[4].y = imageRect.top;
        points[5].x = imageRect.left + 18;
        points[5].y = imageRect.top;
        paint->drawPolyline(fillColor, 1, 0, points, 6);
        FCFont sFont(L"Default", 10, true, false, false);
        drawText(paint, L"xy", fillColor, &sFont, imageRect.left + 4, imageRect.top - 8);
    }
    else if (cName == L"LayoutDiv")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 6, imageRect.right - 1, imageRect.top + 6);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 12, imageRect.right - 1, imageRect.top + 12);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 18, imageRect.right - 1, imageRect.top + 18);
    }
    else if (cName == L"TableLayoutDiv")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 12, imageRect.right - 1, imageRect.top + 12);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 11, imageRect.top, imageRect.left + 11, imageRect.bottom - 1);
    }
    else if (cName == L"Spin")
    {
        FCFont sFont(L"Default", 10, true, false, false);
        paint->drawRect(fillColor, 1, 0, imageRect);
        drawText(paint, L"1", fillColor, &sFont, imageRect.left + 2, imageRect.top);
        drawText(paint, L"2", fillColor, &sFont, imageRect.left + 2, imageRect.top + 10);
        if (true) {
            FCPoint points[3] = {0};
            int mw = 10, mh = 10, dSize = 4;
            points[0].x = imageRect.left + 7 + mw;
            points[0].y = imageRect.top + mh - dSize - 2;
            points[1].x = imageRect.left + 7 + mw - dSize;
            points[1].y = imageRect.top + mh + dSize - 2;
            points[2].x = imageRect.left + 7 + mw + dSize;
            points[2].y = imageRect.top + mh + dSize - 2;
            paint->fillPolygon(fillColor, points, 3);
        }
        if (true) {
            FCPoint points[3] = {0};
            int mw = 10, mh = 10, dSize = 4;
            points[0].x = imageRect.left + 7 + mw;
            points[0].y = mh + imageRect.top + dSize + 10;
            points[1].x = imageRect.left + 7 + mw - dSize;
            points[1].y = imageRect.top + mh - dSize + 10;
            points[2].x = imageRect.left + 7 + mw + dSize;
            points[2].y = imageRect.top + mh - dSize + 10;
            paint->fillPolygon(fillColor, points, 3);
        }
    }
    else if (cName == L"TabView")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        FCRect rect1 = {imageRect.left, imageRect.top, imageRect.left + 11, imageRect.top + 6};
        paint->fillRect(fillColor, rect1);
        FCRect rect2 = {imageRect.left + 11, imageRect.top, imageRect.right, imageRect.top + 7};
        paint->drawRect(fillColor, 1, 0, rect2);
    }
    else if (cName == L"Window")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        FCRect rect1 = {imageRect.left, imageRect.top, imageRect.right - 1, imageRect.top + 6};
        paint->fillRect(fillColor, rect1);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 3, imageRect.top + 11, imageRect.left + 9, imageRect.top + 11);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 3, imageRect.top + 17, imageRect.left + 9, imageRect.top + 17);
        FCRect rect2 = {imageRect.left + 12, imageRect.top + 9, imageRect.left + 21, imageRect.top + 13};
        paint->drawRect(fillColor, 1, 0, rect2);
        FCRect rect3 = {imageRect.left + 12, imageRect.top + 16, imageRect.left + 21, imageRect.top + 20};
        paint->drawRect(fillColor, 1, 0, rect3);
    }
    else if (cName == L"Tree")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        FCRect rect1 = {imageRect.left + 2, imageRect.top + 2, imageRect.left + 8, imageRect.top + 6};
        paint->drawRect(fillColor, 1, 0, rect1);
        FCRect rect2 = {imageRect.left + 4, imageRect.top + 8, imageRect.left + 10, imageRect.top + 12};
        paint->drawRect(fillColor, 1, 0, rect2);
        FCRect rect3 = {imageRect.left + 4, imageRect.top + 14, imageRect.left + 10, imageRect.top + 18};
        paint->drawRect(fillColor, 1, 0, rect3);
        FCRect rect4 = {imageRect.left + 4, imageRect.top + 20, imageRect.left + 10, imageRect.top + 24};
        paint->drawRect(fillColor, 1, 0, rect4);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 12,  imageRect.top + 10, imageRect.left + 22, imageRect.top + 10);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 12, imageRect.top + 16, imageRect.left + 22, imageRect.top + 16);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 12, imageRect.top + 22, imageRect.left + 22, imageRect.top + 22);
    }
    else if (cName == L"Chart")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        int pWidth = (imageRect.right - imageRect.left) / 3;
        FCRect rect1 = {imageRect.left+ 2, imageRect.top + 10, imageRect.left + pWidth, imageRect.bottom - 1};
        paint->fillRect(fillColor, rect1);
        FCRect rect2 = {imageRect.left + pWidth + 1, imageRect.top + 5, imageRect.left + pWidth * 2 - 1, imageRect.bottom - 1};
        paint->fillRect(fillColor, rect2);
        FCRect rect3 = {imageRect.left + pWidth * 2, imageRect.top + 10, imageRect.left + pWidth * 3 - 2, imageRect.bottom - 1};
        paint->fillRect(fillColor, rect3);

    }
    else if (cName == L"Custom")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        FCFont sFont(L"Default", 20, true, false, false);
        drawText(paint, L"U", fillColor, &sFont, imageRect.left + 1, imageRect.top);

    }
    else if (cName == L"BandedGrid")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 11, imageRect.right - 2, imageRect.top + 11);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 16, imageRect.right - 2, imageRect.top + 16);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 6, imageRect.top, imageRect.left + 6, imageRect.bottom - 2);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 12, imageRect.top, imageRect.left + 12, imageRect.bottom - 2);
        paint->drawLine(fillColor, 1, 0, imageRect.left + 18, imageRect.top, imageRect.left + 18, imageRect.bottom - 2);
        FCRect rect1 = {imageRect.left, imageRect.top, imageRect.right - 1, imageRect.top + 6};
        paint->fillRect(fillColor, rect1);
        FCRect rect2 = {imageRect.left, imageRect.top + 6, imageRect.right - 1, imageRect.top + 11};
        paint->fillRect(fillColor, rect2);
    }else if (cName == L"Menu")
    {
        paint->drawRect(fillColor, 1, 0, imageRect);
        FCRect rect1 = {imageRect.left, imageRect.top, imageRect.right - 1, imageRect.top + 6};
        paint->fillRect(fillColor, rect1);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 6, imageRect.right - 1, imageRect.top + 6);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 12, imageRect.right - 1, imageRect.top + 12);
        paint->drawLine(fillColor, 1, 0, imageRect.left, imageRect.top + 18, imageRect.right - 1, imageRect.top + 18);
    }else if (cName == L"Paint")
    {
        paint->drawEllipse(fillColor, 1, 0, imageRect);
    }
}
